﻿namespace Linyou.Net7.WebMinimalAPI.Utility.SwaggerExt
{
    public static class SwaggerExt
    {
        /// <summary>
        /// Swagger 注册
        /// </summary>
        /// <param name="builder"></param>
        public static void AddSwagger(this WebApplicationBuilder builder)
        {
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen(option =>
            {
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    option.SwaggerDoc(version, new()
                    {
                        Title = $"接口开放文档 | {version}",
                        Version = version
                    });
                });
                option.OperationFilter<CustomOperationFilter>();
            });
        }
        /// <summary>
        /// 使用 Swagger
        /// </summary>
        /// <param name="app"></param>
        public static void UserSwagger(this WebApplication app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(option =>
            {
                option.EnableTryItOutByDefault();
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    option.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Linyou.{version}");
                });
            });
        }
    }
}
